<?php
namespace app\home\controller;
use think\Controller;
class Kugou extends Controller
{
    public function _initialize(){
        $this->initialize();
    }
    public function initialize(){
        header('Access-Control-Allow-Origin:*');
        header("Access-Control-Allow-Credentials:true"); 
        header('Access-Control-Request-Method:*');
        header('Access-Control-Allow-Headers:content-type,ly');
        if(request()->isOptions()){
            exit('pass');
        }else{

        }
    }
    //音乐新歌榜
    function getNewList(){
        $url ='http://m.kugou.com/?json=true';
        $data  = get($url);
        $this->response($data);
    }
    //音乐排行榜
    function getRankList(){
        $rankid = Input('rankid',0);
        $page = Input('page',1);
        if($rankid){
            $url ='http://m.kugou.com/rank/info/?rankid='.$rankid.'&page='.$page.'&json=true';
        }else{
            $url ='http://m.kugou.com/rank/list&json=true';
        }
        $data  = get($url);
        $this->response($data);
    }
    //音乐歌单
    function getPlayList(){
        $listid = Input('listid',0);
        if($listid){
            //歌单下的音乐列表
            $url = 'http://m.kugou.com/plist/list/'.$listid.'?json=true';
        }else{
            //全部歌单
            $url ='http://m.kugou.com/plist/index&json=true';
        }
        $data  = get($url);
        $this->response($data);
    }
    //歌手分类
    function getSingerList(){
        $singerid = Input('singerid',0);
        if($singerid){
            //歌手分类下面的歌手列表
            $url = 'http://m.kugou.com/singer/list/'.$singerid.'?json=true';
        }else{
            //歌手分类下面的歌手列表
            $url ='http://m.kugou.com/singer/class&json=true';
        }
        $data  = get($url);
        $this->response($data);
    }

    //歌手信息
    function getSingerInfo(){
        $singerid = Input('singerid',0);
        $url ='http://m.kugou.com/singer/info/'.$singerid.'&json=true';
        $data  = get($url);
        $this->response($data);
    }
    //音乐详情
    function getSongInfo(){
        $hash = Input('hash','');
        $lrc = Input('lrc',1);
        if($lrc){
            $url ='http://www.kugou.com/yy/index.php?r=play/getdata&hash='.$hash;
        }else{
            $url ='http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash='.$hash;
        }
        //echo $url ;
        $data  = get($url);
        $this->response($data);
    }
    //MV详情
    function getMvInfo($hash=''){
        $url ='http://m.kugou.com/app/i/mv.php?cmd=100&hash='.$hash.'&ismp3=1&ext=mp4';
        $data  = get($url);
        $this->response($data);
    }
    //热门搜索列表
    function getHotSearchList($plat=0,$count=30){
        $url ='http://mobilecdn.kugou.com/api/v3/search/hot?format=json&plat='.$plat.'&count='.$count;
        $data  = get($url);
        $this->response($data);
    }
    //搜索列表
    function search($plat=0,$count=30){
        $kw = Input('kw','');
        $page = Input('page',1);
        $pagesize = Input('pagesize',10);
        $url = 'http://mobilecdn.kugou.com/api/v3/search/song?keyword='.$kw.'&page='.$page.'&pagesize='.$pagesize;
        $data  = get($url);
        $data = json_decode($data,true);
        $ret = [];
        foreach ($data['data']['info'] as $key => $value) {
            $ret[]=[
                'album_id'=>$value['album_id'],
                'singername'=>$value['singername'],
                'songname'=>$value['songname'],
                'hash'=>$value['hash'],
                'mvhash'=>$value['mvhash'],
            ];
        }
        $this->response(json_encode($ret));
    }
    function response($data){
        $format = Input('format','json');
        if($format=='json'){
            echo $data;
        }else{
            echo '<pre>';
            print_r($data);
            echo '</pre>';
        }
    }
}
